package com.h3bpm.web.engineEvent;


import OThinker.Common.DotNetToJavaStringHelper;
import OThinker.H3.Entity.EventHandlers.IWorkItemEventHandler;
import OThinker.H3.Entity.IEngine;
import OThinker.H3.Entity.WorkItem.WorkItemModels.WorkItem;
import OThinker.H3.Entity.WorkItem.WorkItemModels.WorkItemFinished;
import OThinker.H3.Entity.WorkItem.WorkItemState;
import OThinker.H3.Entity.WorkflowTemplate.Activity.Activity;
import OThinker.H3.Entity.WorkflowTemplate.WorkflowDocument.PublishedWorkflowTemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MessageWorkItemHandler implements IWorkItemEventHandler {

    private static final Logger logger = LoggerFactory.getLogger(MessageWorkItemHandler.class);

    /**
     * H3BPM 系统的PC端URL地址
     */
    private static String h3bpmPcUrl = "http://127.0.0.1/";

    private static String PORTAL_URL = "https://bpm.hy-online.com/Portal/WorkItemSheets.html?WorkItemID=";

    @Override
    public void OnCreated(IEngine engine, WorkItem workItem) {
        logger.info("《》《》《》@待办 WorkItem WorkItemID={}, State={}", workItem.getWorkItemID(), workItem.getState());

        try {
            //流程模板信息
            PublishedWorkflowTemplate pwt = engine.getWorkflowManager().GetPublishedTemplate(workItem.getWorkflowCode(),workItem.getWorkflowVersion());
            Activity activity = pwt.GetActivityByCode(workItem.getActivityCode());

            if (workItem.getTokenId() == 1){
                logger.info(">>>>>>@TokenId() TokenId={}", workItem.getTokenId());
                return;
            }

            //提交  1.先修改手工  start (取消流程Canceled；取回流程Canceled)
            if(WorkItemState.Finished.equals(workItem.getState()) || WorkItemState.Canceled.equals(workItem.getState())){
                logger.info(">>>>>>@updateMsg WorkItem instanceID={}", workItem.getInstanceID());
                WorkItemFinished workItemFinished = new WorkItemFinished(workItem);
                String requestByWorkItem = MessageHelper.createRequestByCirculate(engine, null,workItemFinished);
                logger.info(">>>>>>@updateMsg 修改消息中心的响应 {}：",requestByWorkItem);

            }

           // 2.给下个节点新增数据
            if(WorkItemState.Waiting.equals(workItem.getState())){
                logger.info(">>>>>>@addMsg提交 WorkItem instanceID={},是否催办Urged = {}", workItem.getInstanceID(),workItem.getUrged());
                String requestByWorkItem = MessageHelper.createRequestByWorkItem(engine, workItem,null,"");
                logger.info(">>>>>>@addMsg提交消息中心的响应 {}：",requestByWorkItem);

            }

            //提交  1.先修改手工  2.给下个节点新增数据 end

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void OnUpdated(IEngine engine, WorkItem workItem) {
            logger.info("<><><>触发OnUpdated<><><> ,WorkItem WorkItemID={}, State={}", workItem.getWorkItemID(), workItem.getState());

        boolean urged = workItem.getUrged();

        try {
            //催办
            if(urged){

                if(WorkItemState.Working.equals(workItem.getState()) || WorkItemState.Waiting.equals(workItem.getState())) {
                    //催办
                    logger.info(">>>>>>进入催办 WorkItem instanceID={}", workItem.getInstanceID());
                    String onUpdated = MessageHelper.createRequestByWorkItem(engine, workItem, null, "OnUpdated");
                    logger.info(">>>>>>催办响应：{}", onUpdated);
                    return;
                }
            }

            if (workItem.getTokenId() == 1){
                logger.info(">>>>>>@TokenId() TokenId={}", workItem.getTokenId());
                return;
            }

            //转办： 1.先修改原消息  2.再新增一条
            if(!DotNetToJavaStringHelper.isNullOrEmpty(workItem.getTransferName())&& WorkItemState.Waiting.equals(workItem.getState())){
                logger.info("<><><>转办任务触发update<><><><");
                logger.info("发送已办任务WorkItem id={}, state={}", workItem.getWorkItemID(), workItem.getState());
                WorkItemFinished workItemFinished = new WorkItemFinished(workItem);
                //1.先修改原消息
                String requestByCirculate = MessageHelper.createRequestByCirculate(engine, null, workItemFinished);
                logger.info("<><><>转办任务修改消息的触发响应<><><>{}",requestByCirculate);
                JSONObject jsonObject = JSON.parseObject(requestByCirculate);
                // 2.再新增一条
                if(jsonObject.getInteger("code") == 200){
                    logger.info("<><><>转办任务新增消息的。。。。");
                    String requestByWorkItem = MessageHelper.createRequestByWorkItem(engine, workItem, null, "");
                    logger.info("<><><>转办任务修改消息的触发响应<><><>{}",requestByWorkItem);
                    return;

                }
                return;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void OnRemoved(IEngine Engine, String ObjId) throws Exception {

    }



}
